#include "StandardInterface.h"
#include "convert_proto_struct.h"
#include "Log.h"
#include "ComServer.h"
#include "ComClient.h"

// __START_REPEATED__
#include "proto/__INTERFACE_FILE__.pb.h"
// __END_REPEATED__

namespace si
{

// __START_REPEATED__
bool StandardInterfaceServer::notify(const __INTERFACE__::__BROADCAST__ &value)
{
	::proto::__INTERFACE_____BROADCAST__ msg;
	if (!StructToProto(value, msg)) {
		LogE("Convert __INTERFACE__::__BROADCAST__ to protobuf failed.");
		return false;
	}
	return notify(msg, __INTERFACE__::__BROADCAST__::get_broadcast_id());
}

bool StandardInterfaceClient::notify(const google::protobuf::Message &msg,
	__INTERFACE__::__BROADCAST__ &value)
{
	auto &interfaces = get_registered_interfaces<__INTERFACE__>();
	if (interfaces.empty()) {
		LogE("The __INTERFACE__ was not registered");
		return false;
	}
	if (!ProtoToStruct(msg, value)) {
		LogE("Convert protobuf to __INTERFACE__::__BROADCAST__ failed.");
		return false;
	}
	for (auto &interface : m_interfaces) {
		interface->notify(value);
	}
	return true;
}

// __END_REPEATED__
bool StandardInterfaceServer::notify(google::protobuf::Message &msg, int id)
{
	MsgDTO dto;
	dto.setCategory(category());
	dto.setDetail(id);
	dto.setValue(&msg);
	m_pComServer->publish(&dto);
	return true;
}

bool StandardInterfaceClient::dispatch(const MsgDTO &dto)
{
	google::protobuf::Message msg;
	switch(dto.detail()) {
// __START_REPEATED__
	case __INTERFACE__::__BROADCAST__::get_broadcast_id():
		dto.value(&msg);
		return notify<__INTERFACE__::__BROADCAST__>(msg);
// __END_REPEATED__
	}
	return false;
}

} // namespace si

